package c.f.d.a.a.b;

import c.f.a.a.a.a;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class e implements X509TrustManager {
    public static final String b = e.class.getSimpleName();
    public List<X509TrustManager> a = new ArrayList();

    public e(InputStream inputStream, String str) throws IllegalArgumentException {
        if (inputStream != null) {
            try {
                TrustManagerFactory instance = TrustManagerFactory.getInstance("X509");
                KeyStore instance2 = KeyStore.getInstance("bks");
                instance2.load(inputStream, str.toCharArray());
                instance.init(instance2);
                TrustManager[] trustManagers = instance.getTrustManagers();
                for (int i = 0; i < trustManagers.length; i++) {
                    if (trustManagers[i] instanceof X509TrustManager) {
                        this.a.add((X509TrustManager) trustManagers[i]);
                    }
                }
            } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException e) {
                String str2 = b;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("loadInputStream: exception : ");
                stringBuilder.append(e.getMessage());
                a.k(str2, stringBuilder.toString());
            } catch (Throwable th) {
                a.g(inputStream);
            }
            a.g(inputStream);
            return;
        }
        throw new IllegalArgumentException("inputstream or trustPwd is null");
    }

    public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
        String str2 = b;
        a.h(str2, "checkClientTrusted: ");
        Iterator it = this.a.iterator();
        while (true) {
            String str3 = "checkServerTrusted CertificateException";
            if (it.hasNext()) {
                try {
                    ((X509TrustManager) it.next()).checkServerTrusted(x509CertificateArr, str);
                    return;
                } catch (CertificateException e) {
                    StringBuilder g = c.b.a.a.a.g(str3);
                    g.append(e.getMessage());
                    a.k(str2, g.toString());
                }
            } else {
                throw new CertificateException(str3);
            }
        }
    }

    public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException {
        StringBuilder g;
        String str2 = b;
        StringBuilder g2 = c.b.a.a.a.g("checkServerTrusted begin ,server ca chain size is : ");
        g2.append(x509CertificateArr.length);
        g2.append(" ,auth type is : ");
        g2.append(str);
        a.h(str2, g2.toString());
        for (X509Certificate x509Certificate : x509CertificateArr) {
            g = c.b.a.a.a.g("server ca chain: getSubjectDN is :");
            g.append(x509Certificate.getSubjectDN());
            g.append(" , getIssuerDN :");
            g.append(x509Certificate.getIssuerDN());
            g.toString();
            g = new StringBuilder();
            g.append("server ca chain is :");
            g.append(x509Certificate);
            g.toString();
        }
        int size = this.a.size();
        int i = 0;
        while (i < size) {
            try {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("check server i : ");
                stringBuilder.append(i);
                a.h(str2, stringBuilder.toString());
                X509TrustManager x509TrustManager = (X509TrustManager) this.a.get(i);
                X509Certificate[] acceptedIssuers = x509TrustManager.getAcceptedIssuers();
                if (acceptedIssuers != null) {
                    StringBuilder stringBuilder2 = new StringBuilder();
                    stringBuilder2.append("client root ca size is : ");
                    stringBuilder2.append(acceptedIssuers.length);
                    a.h(str2, stringBuilder2.toString());
                    for (X509Certificate issuerDN : acceptedIssuers) {
                        StringBuilder stringBuilder3 = new StringBuilder();
                        stringBuilder3.append("client root ca getIssuerDN :");
                        stringBuilder3.append(issuerDN.getIssuerDN());
                        stringBuilder3.toString();
                    }
                }
                x509TrustManager.checkServerTrusted(x509CertificateArr, str);
                stringBuilder = new StringBuilder();
                stringBuilder.append("checkServerTrusted succeed ,root ca issuer is : ");
                stringBuilder.append(x509CertificateArr[x509CertificateArr.length - 1].getIssuerDN());
                a.h(str2, stringBuilder.toString());
                return;
            } catch (CertificateException e) {
                g = c.b.a.a.a.g("checkServerTrusted error :");
                g.append(e.getMessage());
                g.append(" , time : ");
                g.append(i);
                a.k(str2, g.toString());
                if (i == size - 1) {
                    if (x509CertificateArr.length > 0) {
                        StringBuilder g3 = c.b.a.a.a.g("root ca issuer : ");
                        g3.append(x509CertificateArr[x509CertificateArr.length - 1].getIssuerDN());
                        a.k(str2, g3.toString());
                    }
                    throw e;
                }
                i++;
            }
        }
    }

    public X509Certificate[] getAcceptedIssuers() {
        try {
            ArrayList arrayList = new ArrayList();
            for (X509TrustManager acceptedIssuers : this.a) {
                arrayList.addAll(Arrays.asList(acceptedIssuers.getAcceptedIssuers()));
            }
            return (X509Certificate[]) arrayList.toArray(new X509Certificate[arrayList.size()]);
        } catch (Exception e) {
            String str = b;
            StringBuilder g = c.b.a.a.a.g("getAcceptedIssuers exception : ");
            g.append(e.getMessage());
            a.k(str, g.toString());
            return new X509Certificate[0];
        }
    }
}
